<template>
  <!-- 对话框表单 -->
  <el-dialog class="ba-operate-dialog"
             :close-on-click-modal="false"
             :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
             @close="baTable.toggleForm"
             :destroy-on-close="true">
    <template #header>
      <div class="title"
           v-drag="['.ba-operate-dialog', '.el-dialog__header']"
           v-zoom="'.ba-operate-dialog'">
        {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
      </div>
    </template>
    <el-scrollbar v-loading="baTable.form.loading"
                  class="ba-table-form-scrollbar">
      <div class="ba-operate-form"
           :class="'ba-' + baTable.form.operate + '-form'"
           :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'">
        <el-form ref="formRef"
                 @keyup.enter="baTable.onSubmit(formRef)"
                 :model="baTable.form.items"
                 :label-position="config.layout.shrink ? 'top' : 'right'"
                 :label-width="baTable.form.labelWidth + 'px'"
                 :rules="rules"
                 v-if="!baTable.form.loading">
          <el-form-item label="店铺">
            <el-select class="w100"
                       v-model="baTable.form.items!.shop_id"
                       :placeholder="t('Please select field', { field: '店铺' })"
                       clearable>
              <el-option :label="v.shop_name"
                         :value="v.shop_id"
                         v-for="v in shopList"
                         :key="v.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="运营"
                        prop="admin_id">
            <el-select class="w100"
                       v-model="baTable.form.items!.admin_id"
                       :placeholder="t('Please select field', { field: '运营' })"
                       clearable>
              <el-option :label="v.nickname"
                         :value="v.id"
                         v-for="v in operationsList"
                         :key="v.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="广告账户">
            <el-select class="w100"
                       filterable
                       v-model="baTable.form.items!.advertiser_id"
                       :placeholder="t('Please select field', { field: '广告账户' })"
                       clearable>
              <el-option v-for="v in advaccountList"
                         :key="v.id"
                         :label="v.advertiser_id"
                         :value="v.id">
                <span style="float: left">{{ v.advertiser_id }}</span>
                <span style=" float: right;color: #909399;font-size: 12px; ">{{ v.advertiser_name
 }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <FormItem label="开始时间"
                    v-model="baTable.form.items!.createtime"
                    type="datetime"
                    prop="createtime"
                    :placeholder="t('Please select field', { field: '开始时间' })" />
          <FormItem label="结束时间"
                    v-model="baTable.form.items!.updatetime"
                    type="datetime"
                    prop="updatetime"
                    :placeholder="t('Please select field', { field: '结束时间' })" />

                    
        <el-form-item label="附加运营"
                      prop="affiliated">
          <el-input v-model="baTable.form.items!.affiliated"/>
        </el-form-item>

          <el-form-item label="状态"
                        prop="status">
            <el-radio-group v-model="baTable.form.items!.status">
              <el-radio value="normal"
                        border>在用</el-radio>
              <el-radio value="hidden"
                        border>过期</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
    </el-scrollbar>
    <template #footer>
      <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
        <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
        <el-button v-blur
                   :loading="baTable.form.submitLoading"
                   @click="baTable.onSubmit(formRef)"
                   type="primary">
          {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, inject, watch, onBeforeMount} from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import type { FormInstance, FormItemRule } from 'element-plus'
import FormItem from '/@/components/formItem/index.vue'
import { useAdminInfo } from '/@/stores/adminInfo'
import { useConfig } from '/@/stores/config'
import { getAdvaccountList, getShopList, getOperationsList }  from '/@/api/backend/common'

const { t } = useI18n()
const config = useConfig()
const adminInfo = useAdminInfo()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass
const advaccountList = ref([] as any[])
const shopList = ref([] as any[])
const operationsList = ref([] as any[])
const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    admin_id: [buildValidatorData({ name: 'required', title: '运营', message: '请选择运营' })]
})

watch(
    () => baTable.form.operate,
    (newVal) => {
        if(newVal) getList()
    }
)

const getList = () => {
    Promise.all([getAdvaccountList(), getShopList(), getOperationsList()]).then((res:any[]) => {
        const [adList, spList, opList] = res
        advaccountList.value = adList.data
        shopList.value = spList.data.list
        operationsList.value = opList.data.list
    })
}


</script>